package com.logictech.lims.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import com.logictech.lims.service.LogService;
import com.logictech.lims.entity.Experiment;
import com.logictech.lims.entity.ExperimentMaterial;
import com.logictech.lims.entity.Glassware;
import com.logictech.lims.entity.GlasswareType;
import com.logictech.lims.entity.Location;
import com.logictech.lims.entity.Log;
import com.logictech.lims.entity.Position;
import com.logictech.lims.entity.PreparationInventory;
import com.logictech.lims.entity.PreparationType;
import com.logictech.lims.entity.ReagentInventory;
import com.logictech.lims.entity.ReagentType;
import com.logictech.lims.entity.Sourcing;
import com.logictech.activity.system.entity.SystemRole;
import com.logictech.activity.system.entity.SystemUser;
import com.logictech.activity.system.vo.LoginUser;
import com.logictech.common.util.data.DateUtils;
import com.logictech.lims.dao.LogDao;

@Service("logService")
public class LogServiceImpl implements LogService {
	@Resource
	private LogDao logDao;

	/**
	 * flag 1:新增，2:修改，3:删除
	 */
	@Override
	public void doAdd(String flag, Object o) {
		LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
		
		Log log = new Log();
		
		if("1".equals(flag)){
			if(o instanceof PreparationInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "配制试液"+o.toString());
			}else if(o instanceof PreparationType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增试液类型"+o.toString());
			}else if(o instanceof ReagentInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增试剂"+o.toString());
			}else if(o instanceof ReagentType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增试剂类型"+o.toString());
			}else if(o instanceof GlasswareType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增玻璃仪器类型"+o.toString());
			}else if(o instanceof Glassware){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增玻璃仪器"+o.toString());
			}else if(o instanceof Position){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增位置"+o.toString());
			}else if(o instanceof Location){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增库位"+o.toString());
			}else if(o instanceof Experiment){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增实验"+o.toString());
			}else if(o instanceof ExperimentMaterial){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增实验材料"+o.toString());
			}else if(o instanceof Sourcing){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增采购需求"+o.toString());
			}else if(o instanceof SystemUser){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增用户"+o.toString());
			}else if(o instanceof SystemRole){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "新增角色"+o.toString());
			}
		} else if("2".equals(flag)){
			if(o instanceof PreparationInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改试液"+o.toString());
			}else if(o instanceof PreparationType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改试液类型"+o.toString());
			}else if(o instanceof ReagentInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改试剂"+o.toString());
			}else if(o instanceof ReagentType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改试剂类型"+o.toString());
			}else if(o instanceof GlasswareType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改玻璃仪器类型"+o.toString());
			}else if(o instanceof Glassware){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改玻璃仪器"+o.toString());
			}else if(o instanceof Position){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改位置"+o.toString());
			}else if(o instanceof Location){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改库位"+o.toString());
			}else if(o instanceof Experiment){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改实验"+o.toString());
			}else if(o instanceof ExperimentMaterial){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改实验材料"+o.toString());
			}else if(o instanceof Sourcing){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改采购需求"+o.toString());
			}else if(o instanceof SystemUser){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改用户"+o.toString());
			}else if(o instanceof SystemRole){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "修改角色"+o.toString());
			}
		} else if("3".equals(flag)){
			if(o instanceof PreparationInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除试液"+o.toString());
			}else if(o instanceof PreparationType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除试液类型"+o.toString());
			}else if(o instanceof ReagentInventory){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除试剂"+o.toString());
			}else if(o instanceof ReagentType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除试剂类型"+o.toString());
			}else if(o instanceof GlasswareType){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除玻璃仪器类型"+o.toString());
			}else if(o instanceof Glassware){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除玻璃仪器"+o.toString());
			}else if(o instanceof Position){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除位置"+o.toString());
			}else if(o instanceof Location){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除库位"+o.toString());
			}else if(o instanceof Experiment){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除实验"+o.toString());
			}else if(o instanceof ExperimentMaterial){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除实验材料"+o.toString());
			}else if(o instanceof Sourcing){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除采购需求"+o.toString());
			}else if(o instanceof SystemUser){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除用户"+o.toString());
			}else if(o instanceof SystemRole){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "删除角色"+o.toString());
			}
		} else if("4".equals(flag)){
			if(o instanceof Sourcing){
				log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS) + " " + user.getUserName() + "确认收货"+o.toString());
			}
		}
		
		logDao.add(log);
	}
	
	@Override
	public void doAdd(String strLog) {
		Log log = new Log();
		log.setContent(DateUtils.dateToString(DateUtils.PATTEN_HMS)+ " " + strLog);
		logDao.add(log);
	}

	@Override
	public void doEdit(Log log) {
		logDao.update(log);
	}

	@Override
	public void doDelete(String id) {
		logDao.delete(id);
	}

	@Override
	public Log queryById(String id) {
		Log log  = logDao.get(id);
		return log;
	}

	@Override
	public PageList<Log> queryPageList(
		PageQuery<Log> pageQuery) {
		PageList<Log> result = new PageList<Log>();
		Integer itemCount = logDao.count(pageQuery);
		List<Log> list = logDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	public List<Log> queryList(String logContent) {
		
		return logDao.queryList(logContent);
	}
	
}
